package aaaBrowser;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.datatransfer.Clipboard;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import aaBaolilo.ImageDemo;
import aaBaolilo.OcrX;
import aaaBrowser.test.Reg;

import com.attilax.img.imgx;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.ClipboardUtil;
import com.attilax.lang.IniEx;
import com.attilax.shbye.YejmX;
import com.attilax.util.dirx;
@SuppressWarnings("all")
public class CaptX {

	//YejmX yx
public static YejmX captx_uxx;
	private void iniCaptchRecong() {
	//	BufferedImage img=ImageIO.read(new File(pathname));
		System.out.println(" ini capt u");
		captx_uxx=new YejmX();
		try {
			captx_uxx.inix();
		} catch (IniEx e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	//	System.out.println( captx.getResult(img));
		
	}
	public static void main(String[] args) throws IOException {
		BufferedImage img33=ImageIO.read(new File(pathx.classPath(Reg.class)+"\\a.png"));
		Image img = (Image )img33; 
		
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();//得到系统的剪贴板   
		 imgx.setClipboardImage(img);
		CaptX x=new CaptX();
		System.out.println(x.getTxt());
		System.out.println("--ok");
	}

	private char[] getTxt(BufferedImage img) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getTxt() {
		
		System.out.println("aaaabbbcccdddd00000aa");
		 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();//得到系统的剪贴板   
			//获取粘贴板图片
			Image image = null;
			try {
				image = ClipboardUtil.getImageFromClipboard();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
			
			String basename=filex.getUUidName();
			String saveTempDir =pathx.classPathParent()+"\\tmp\\";
		 filex.createAllPath(saveTempDir+"aa.txt");
					//"c:\\";
			String jpg = saveTempDir+basename+".jpg";
			System.out.println(jpg);
			//image.get
			File file= new File(jpg);
			//转成jpg
			 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
			//转成png
		//	BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, null, null);
			//ImageIO.write((RenderedImage)bufferedImage, "jpg", file);
			try {
				ImageIO.write((RenderedImage)bufferedImage, "jpg", file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//	String deboxJpg ="c:\\"+ basename+"_deBox.jpg";
			String bin_jpg = saveTempDir+basename+"_bin.jpg";
			 try {
//					Thumbnails.of(jpg)
//				    //从原图哪里开始裁剪   裁减多少
//				    .sourceRegion(Positions.CENTER, 50, 23)
//				    //新图的大小
//				    .size(50, 23).toFile(deboxJpg);
				
				
				ImageDemo demo = new ImageDemo();
				 	
					demo.binaryImage(jpg,bin_jpg);
//				Thumbnails.of(jpg)
//				    //从原图哪里开始裁剪   裁减多少
//				    .sourceRegion(Positions.TOP_LEFT, 400, 400)
//				    //新图的大小
//				    .size(50, 23).toFile(deboxJpg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		 String captchCode=null;
		 try {
			 captchCode= OcrX.getCaptch(bin_jpg);
			 captchCode = captchCode.replaceAll(" ", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		 if(captchCode==null || captchCode.length()!=4)
		 {
			 BufferedImage bimg=toBufferedImage(image);
			 if(captx_uxx==null)
				 iniCaptchRecong();
			 return captx_uxx.getResult(bimg);
		 }
		return captchCode;
	}
	
	
	public static BufferedImage toBufferedImage(Image image) {
	    if (image instanceof BufferedImage) {
	        return (BufferedImage)image;
	     }
	 
	    // This code ensures that all the pixels in the image are loaded
	     image = new ImageIcon(image).getImage();
	 
	    // Determine if the image has transparent pixels; for this method's
	    // implementation, see e661 Determining If an Image Has Transparent Pixels
	    //boolean hasAlpha = hasAlpha(image);
	 
	    // Create a buffered image with a format that's compatible with the screen
	     BufferedImage bimage = null;
	     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    try {
	        // Determine the type of transparency of the new buffered image
	        int transparency = Transparency.OPAQUE;
	       /* if (hasAlpha) {
	         transparency = Transparency.BITMASK;
	         }*/
	 
	        // Create the buffered image
	         GraphicsDevice gs = ge.getDefaultScreenDevice();
	         GraphicsConfiguration gc = gs.getDefaultConfiguration();
	         bimage = gc.createCompatibleImage(
	         image.getWidth(null), image.getHeight(null), transparency);
	     } catch (HeadlessException e) {
	        // The system does not have a screen
	     }
	 
	    if (bimage == null) {
	        // Create a buffered image using the default color model
	        int type = BufferedImage.TYPE_INT_RGB;
	        //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
	        /*if (hasAlpha) {
	         type = BufferedImage.TYPE_INT_ARGB;
	         }*/
	         bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
	     }
	 
	    // Copy image to buffered image
	     Graphics g = bimage.createGraphics();
	 
	    // Paint the image onto the buffered image
	     g.drawImage(image, 0, 0, null);
	     g.dispose();
	 
	    return bimage;
	}

}
